# import uuid
# from spider_framework import Request, Response, Item, Spider
# from spider_framework.get_day import timestamp_for_ms
# from comm.jiedian_base import JieDian
# from spider_framework.log import DBLog



# class JieDianDetail(Spider, JieDian):
#     name = 'jiedian_detail'
#     start_urls = []

#     def __init__(self, db):
#         super(JieDianDetail, self).__init__()
#         self.db = db
#         self.shop_data_list = None
#         self.req = self.gen_req()

#     async def get_shop_id_from_db(self):
#         sql = f'''
#         select a.* from
#     (select shop_id,search_lat,search_lng from jiedian_shop_poi) as a left join
#     (select shop_id from jiedian_shop_detail) as b on a.shop_id=b.shop_id
#     where b.shop_id is null
#         '''
#         res_list = await self.db.select(sql)
#         shop_data_list = []
#         while res_list:
#             sub_shop_data_list = []
#             for i in range(20):
#                 if res_list:
#                     sub_shop_data_list.append(res_list.pop(0))
#             shop_data_list.append(sub_shop_data_list)
#         self.shop_data_list = shop_data_list

#     def gen_shop(self):
#         for sub_shop_data in self.shop_data_list:
#             shop_list = []
#             lat = None
#             lng = None
#             for shop in sub_shop_data:
#                 if not lat:
#                     lat = float(shop['search_lat'])
#                     lng = float(shop['search_lng'])
#                 shop_list.append(shop['shop_id'])
#             yield lat, lng, shop_list

#     async def before_request(self):
#         self.log = DBLog(db=self.db, spider_name=self.name, crawl_version=self.version)
#         await self.get_shop_id_from_db()

#     def gen_req(self):
#         poi_list = self.gen_shop()
#         url = 'https://api.ankerjiedian.com/index.php'
#         headers = {
#             'content-type': 'application/json; charset=UTF-8',
#             'accept-encoding': 'gzip',
#             'user-agent': 'okhttp/3.8.0'
#         }
#         for lat, lng, shop_id_list in poi_list:
#             t = timestamp_for_ms()
#             utoken = self.create_u_token(lng, lat, t)
#             session_authorization = self.create_session_authorization(f'{lng},{lat}')
#             data = {
#                 "body": {
#                     "gps_latitude": lat,
#                     "gps_longitude": lng,
#                     "latitude": lat,
#                     "longitude": lng,
#                     "special_return_text": 1,
#                     "uuid": shop_id_list
#                 },
#                 "header": {
#                     "access_token": "",
#                     "api": "YBusiness.getBusinessList",
#                     "applet": "android",
#                     "brand": "xiaomi",
#                     "client_v": "3.710",
#                     "dpi": "1080,1920",
#                     "model": "mi 5",
#                     "msg_id": str(uuid.uuid4()),
#                     "sensor_param": {
#                         "$referrer": "sharepower://page/home",
#                         "$url": "sharepower://page/home"
#                     },
#                     "service": "sharedCharging",
#                     "session_authorization": session_authorization,
#                     "session_id": str(uuid.uuid4()),
#                     "type": "",
#                     "ugen": "",
#                     "userAgent": "jiedian/ client_v/3.710 (MI 5; Android; Android OS ; 6.0.1; zh) ApacheHttpClient/4.0",
#                     "utime": t,
#                     "utoken": utoken
#                 },
#                 "isTokenIntercept": True
#             }
#             yield Request(url=url, headers=headers, callback=self.parse, method='post', json=data,
#                           text_check=['请求出错', 'Access Denied'], use_proxy=False, meta={'lat': lat, 'lng': lng})

#     def start_requests(self):
#         for i in range(100):
#             try:
#                 yield next(self.req)
#             except StopIteration:
#                 yield None

#     def parse(self, response: Response):
#         shop_detail = response.json
#         shop_detail_list = shop_detail.get('body')
#         data_list = []
#         for shop in shop_detail_list:
#             shop_id = shop.get('uuid')
#             shop_name = shop.get('name')
#             shop_lat = shop.get('lat')
#             shop_lng = shop.get('lng')
#             telephone = shop.get('telephone')
#             address = shop.get('address')
#             opening_hours = shop.get('opening_hours')
#             city = shop.get('city')
#             data_list.append([
#                 shop_id,
#                 shop_name,
#                 shop_lat,
#                 shop_lng,
#                 telephone,
#                 address,
#                 opening_hours,
#                 city
#             ])
#         if data_list:
#             yield Item(data_list=data_list, table_name='jiedian_shop_detail', db=self.db,
#                        db_col=['shop_id', 'shop_name', 'shop_lat', 'shop_lng', 'telephone', 'address', 'opening_hours',
#                                'city'],
#                        replace_str='"None"', insert_col=[],
#                        is_replace=True, unique_key=['shop_id'])
#         try:
#             yield next(self.req)
#         except StopIteration:
#             yield None
